// Copyright (C) 2024 wlly-lzh
// Created by wlly-lzh on 24-5-4
// Sort 排序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

void Error_Sort_Malloc();

void BubbleSort_Lds(void *Array,size_t Length,size_t Size,
		int (*Compare)(const void *,const void *)){
	bool flag=false;
	void *Ex=malloc(sizeof(Size));
	if(Ex == NULL)
		Error_Sort_Malloc();
	for(size_t i=0; i<Length; ++i){
		for(size_t t=1; t<Length-i; ++t){
			if((*Compare)(Array+(t-1)*Size, Array+t*Size) > 0){
				memcpy(Ex, Array+(t-1)*Size, Size);
				memcpy(Array+(t-1)*Size, Array+t*Size, Size);
				memcpy(Array+t*Size, Ex, Size);
				flag=true;
			}
		}
		if(flag == false)
			break;
		else
			flag=false;
	}
	free(Ex);
}

#ifdef _LINUX_PLATFORM_
void Error_Sort_Malloc(){
	fprintf(stderr,"\e[31mliblds/Sort: Malloc False!\e[0m\n");
	exit(3);
}
#else
void Error_Sort_Malloc(){
	fprintf(stderr,"liblds/Sort: Malloc False!\n");
	exit(3);
}
#endif
